using System;
using RomanJendrusz.Amazon.ETL.Exceptions;
namespace RomanJendrusz.Amazon.ETL.Messages
{
    public abstract class AmazonResponse : IEquatable<AmazonResponse>
    {
        private Guid correlationId;
        
        protected AmazonResponse(Guid correlationId)
        {
            this.correlationId = correlationId;
        }

        public Guid CorrelationId
        {
            get { return correlationId; }
            set { correlationId = value; }
        }
        
        private object additionalInformations;

        public object AdditionalInformations { set { additionalInformations = value; } }

        public bool IsValid { get; set; }

        public T As<T>() where T: AmazonResponse
        {
            var response = this as T;
            if(response == null)
            {
                throw new InvalidResponseCastException(GetType(), typeof (T));
            }
            return response;
        }

        public override string ToString()
        {
            return string.Format("Message of type {0} that is valid {1} \r\n Additional Infromations:{2}", GetType().FullName,
                          IsValid, additionalInformations);

        }


        public bool Equals(AmazonResponse amazonResponse)
        {
            if (amazonResponse == null) return false;
            return Equals(correlationId, amazonResponse.correlationId) && Equals(IsValid, amazonResponse.IsValid);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as AmazonResponse);
        }

        public override int GetHashCode()
        {
            return correlationId.GetHashCode() + 29*IsValid.GetHashCode();
        }
    }
}